<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;



use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use app\common\model\UserCoin;
use app\common\model\UserPlatform;
use think\Db;

/**
 * 用户接口逻辑
 */
class Quotation extends ApiBase
{
    

    
    /**
     * 数字资产
     */
    public function recommended_currencies($data = [])
    {
        $list=$this->modelQuotationCoin->getList(['status'=>1],'id,platform_id,coin_b,coin_a,transaction_pairs','id desc',false);
        $lis=[];
        if(count($list)>9){
            $arr=array_rand($list,9);

            foreach ($arr as $k=>$v){
                $list[$v]['image']=$list[$v]->image;
                $list[$v]['platform_name']=$list[$v]->platform['name'];
                $list[$v]['coin_name']=$list[$v]->coin_a;
                $list[$v]['coin_text']=$list[$v]->coin_a;
                $lis[]=$list[$v];
                unset($list[$v]->platform);
            }
        }else{
            foreach ($list as $k=>$v){

                $v['image']=$v->image;
                $v['platform_name']=$v->platform['name'];
                $v['coin_name']=$v->coin_a;
                $v['coin_text']=$v->coin_a;
                $lis[]=$v;
                unset($v->platform);
            }
        }



        return $lis;


    }


    //关注/取消关注币种

    public function follow_coin($data){
        $ids=explode(',',$data['ids']);
        $uid=decoded_user_token($data['user_token'],'member_id');
        if($data['type']==1){
            $arr=[];
          foreach ($ids as $k=>$v){
              if(!Db::name('user_coin')->where(['user_id'=>$uid,'coin_id'=>$v])->find()){
                  $info['user_id']=$uid;
                  $info['create_time']=time();
                  $info['coin_id']=$v;
                  $arr[]=$info;
              }

          }

          Db::name('user_coin')->insertAll($arr);
            return ajaxreturn(0,lang('关注成功'));
        }else{
            Db::name('user_coin')->where(['user_id'=>$uid,'coin_id'=>['in',$ids]])->delete();
            return ajaxreturn(0,lang('取消关注成功'));
        }

    }

    //排序币种

    public function sort_coin($data){
        $ids=explode(',',$data['ids']);
        $count=count($ids);
       // $uid=decoded_user_token($data['user_token'],'member_id');
        $user_coin=new UserCoin();

            $arr=[];
            foreach ($ids as $k=>$v){

                    $info['id']=$v;
                    $info['update_time']=time();
                    $info['sort']=$count-$k;
                    $arr[]=$info;


            }
        $user_coin->saveAll($arr);
        return ajaxreturn(0,lang('置顶成功'));


    }

    //关注/取消关注平台

    public function follow_platform($data){
        $ids=explode(',',$data['ids']);
        $uid=decoded_user_token($data['user_token'],'member_id');
        if($data['type']==1){
            $arr=[];
            foreach ($ids as $k=>$v){
                if(!Db::name('user_platform')->where(['user_id'=>$uid,'platform_id'=>$v])->find()){
                    $info['user_id']=$uid;
                    $info['create_time']=time();
                    $info['platform_id']=$v;
                    $arr[]=$info;
                }

            }

            Db::name('user_platform')->insertAll($arr);
            return ajaxreturn(0,lang('关注成功'));
        }else{
            Db::name('user_platform')->where(['user_id'=>$uid,'platform_id'=>['in',$ids]])->delete();
            return ajaxreturn(0,lang('取消关注成功'));
        }

    }


    //排序平台

    public function sort_platform($data){
        $ids=explode(',',$data['ids']);
        $count=count($ids);

        // $uid=decoded_user_token($data['user_token'],'member_id');
        $user_coin=new UserPlatform();

        $arr=[];
        foreach ($ids as $k=>$v){

            $info['id']=$v;
            $info['update_time']=time();
            $info['sort']=$count-$k;
            $arr[]=$info;


        }

        $user_coin->saveAll($arr);
        return ajaxreturn(0,lang('置顶成功'));


    }













}
